#!/usr/bin/python2
# -*- coding: utf-8 -*-

import math, sys
from PySide import QtCore, QtGui

class Overlay(QtGui.QWidget):
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)
        palette = QtGui.QPalette(self.palette())
        palette.setColor(palette.Background, QtCore.Qt.transparent)
        self.setPalette(palette)
        self.counter = 0
        self.timer = 0
        self.style = 3
        self.painter = QtGui.QPainter()
        self.background_brush = QtGui.QBrush(QtGui.QColor(0, 0, 0, 180))

    def paintEvent(self, event):
        self.painter.begin(self)
        self.painter.setRenderHint(QtGui.QPainter.Antialiasing)
        self.painter.fillRect(event.rect(), self.background_brush)
        self.painter.setPen(QtGui.QPen(QtCore.Qt.NoPen))

        current = ( self.counter / 5 ) % 6

        if self.style == 0:
            for i in range(6):
                tsprt = i - current
                if tsprt < 0:
                    tsprt = 5 + tsprt
                self.painter.setBrush(QtGui.QBrush(QtGui.QColor(255, 255, 255, 45 * tsprt + 10)))
                self.painter.drawEllipse(
                        self.width()/2 + 30 * math.sin(-2 * math.pi * i / 6.0) - 20,
                        self.height()/2 + 30 * math.cos(-2 * math.pi * i / 6.0) - 20,
                        tsprt * (10 / 6.0 ) + 10,
                        tsprt * (10 / 6.0) + 10
                        )
        elif self.style == 1:
            # scattered line
            for i in range(6):
                tsprt = i - current
                if tsprt < 0:
                    tsprt = 5 + tsprt
                self.painter.setBrush(QtGui.QBrush(QtGui.QColor(255, 255, 255, 45 * tsprt + 10)))
                self.painter.drawEllipse(
                        self.width()/2 - 60 + 20*i,
                        self.height()/2,
                        10,
                        10
                        )
        elif self.style == 2:
            # line
            for i in range(6):
                tsprt = i - current
                if tsprt < 0:
                    tsprt = 5 + tsprt
                self.painter.setBrush(QtGui.QBrush(QtGui.QColor(255, 255, 255, 45 * tsprt + 10)))
                self.painter.drawRect(
                        self.width()/2 - 60 + 20*i,
                        self.height()/2,
                        10,
                        5
                        )
        elif self.style == 3:
            pen = QtGui.QPen()
            pen.setWidth(10)
            pen.setCapStyle(QtCore.Qt.RoundCap)
            for i in range(6):
                tsprt = i - current
                if tsprt < 0:
                    tsprt = 5 + tsprt
                pen.setColor(QtGui.QColor(255, 255, 255, 45 * tsprt + 10))
                self.painter.setPen(pen)
                self.painter.drawLine(
                        self.width()/2 + 20 * math.sin(-2 * math.pi * i / 6.0),
                        self.height()/2 + 20 * math.cos(-2 * math.pi * i / 6.0),
                        self.width()/2 + 40 * math.sin(-2 * math.pi * i / 6.0),
                        self.height()/2 + 40 * math.cos(-2 * math.pi * i / 6.0)
                    )

        self.painter.end()

    def showEvent(self, event):
        if self.timer == 0:
            self.timer = self.startTimer(30)

    def timerEvent(self, event):
        self.counter += 1
        if self.counter > 60:
            self.counter = 0
        self.update()

    def stop(self):
            self.killTimer(self.timer)
            self.timer = 0
            self.hide()

if __name__ == "__main__":

    class MainWindow(QtGui.QMainWindow):

        def __init__(self, parent = None):

            QtGui.QMainWindow.__init__(self, parent)

            widget = QtGui.QWidget(self)
            self.editor = QtGui.QTextEdit()
            self.editor.setPlainText("0123456789"*100)
            layout = QtGui.QGridLayout(widget)
            layout.addWidget(self.editor, 0, 0, 1, 3)
            button = QtGui.QPushButton("Wait")
            layout.addWidget(button)
            button1 = QtGui.QPushButton("Stop")
            layout.addWidget(button1)

            self.setCentralWidget(widget)
            self.overlay = Overlay(self.editor)
            self.overlay = Overlay(button1)
            self.overlay.hide()

            button.clicked.connect(self.overlay.show)
            button1.clicked.connect(self.overlay.stop)

        def resizeEvent(self, event):
            self.overlay.resize(event.size())
            event.accept()

    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())


